package com.hltx.controller.app.index;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hltx.controller.base.BaseController;
import com.hltx.pojo.business.BusinessInfo;
import com.hltx.service.index.AppIndexManager;
import com.hltx.util.ResultInfo;





/**
 * APP首页controller
 * 
 * @author shibeilei
 *
 */
@Controller
@RequestMapping(value = "/app/")
public class AppIndexController extends BaseController{

	@Resource(name="appIndexService")
	private AppIndexManager appIndexService;

	@RequestMapping(value = "/index", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String,Object> index(BusinessInfo bi,
			HttpServletRequest request, HttpSession session){
		this.logBefore(logger, "APP首页-开始");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try{
			
			//List<BusinessClass> bcList = appIndexService.getBusinessClass(0);
			List<BusinessInfo> biList;

			int start = (bi.getPage()-1) * bi.getShowNumber();
			bi.setStart(start);
			boolean isLngLat = false;
			if(bi.getCurCityId() != 0){
				if(bi.getCityId() == 0 || bi.getCityId() == bi.getCurCityId()){
					isLngLat = true;
				}
			}
			
			if(isLngLat){
				biList = appIndexService.getNearBusinessByLngLat(bi);
			}else{
				biList = appIndexService.getDisBusiness(bi);
			}
			
			resMap.put("biList", biList);
			//resMap.put("bcList", bcList);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		}catch(Exception e){
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resMap;
	}
	
	
	/**
	 * 打开首页
	 */
	@RequestMapping("/wap/index")
	public String index() {
		return "redirect:/index";
	}
	
	
	/**
	 * 打开购物券商城
	 */
	@RequestMapping(value = { "/wap/vip", "/user/wap/vip" })
	public String vip() {
		return "redirect:/vip";
	}	
		
	
	@RequestMapping(value = "/search", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String,Object> search(BusinessInfo bi,
			HttpServletRequest request, HttpSession session){
		this.logBefore(logger, "APP首页-开始");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try{
			
			List<BusinessInfo> biList;

			int start = (bi.getPage()-1) * bi.getShowNumber();
			bi.setStart(start);
			
			boolean isLngLat = false;
			if(bi.getCurCityId() != 0){
				if(bi.getCityId() == 0 || bi.getCityId() == bi.getCurCityId()){
					isLngLat = true;
				}
			}
			
			if(isLngLat){
				biList = appIndexService.getNearBusinessByLngLat(bi);
			}else{
				biList = appIndexService.getDisBusiness(bi);
			}
			
			resMap.put("biList", biList);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		}catch(Exception e){
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resMap;
	}
	
	@RequestMapping(value = "/getBusinessInfo", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> getBusinessInfo(@RequestParam("businessId")int businessId){
		this.logBefore(logger, "APP商家详情-开始");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try{
			
			BusinessInfo bi = appIndexService.getBusinessInfo(businessId);
			
			resMap.put("bi", bi);
		}catch(Exception e){
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		
		
		return resMap;
	}
	
}
